package com.javamail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.junit.Test;

public class TestJavaMail {
	
	@Test
	public void test1() throws Exception {
		/*
		 * 1.得到Session对象
		 * 
		 */
		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.qq.com");
		props.setProperty("mail.smtp.auth", "true");
		Authenticator authenticator = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("", "");
			}
			
		};
		Session session = Session.getInstance(props, authenticator);
		
		/*
		 * 2.MimeMessage
		 */
		
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("1321466573@qq.com"));//设置发件人
		msg.setRecipients(RecipientType.TO, "jerryjoejj@hotmail.com");//设置收件人
		msg.setSubject("这是一封测试邮件");//设置邮件主题
		msg.setContent("这是一封测试邮件", "text/html;charset=utf-8");//设置邮件正文
		
		Transport.send(msg);
		
	}
}
